package com.yonyou.ucf.mdf;

import com.yonyou.diwork.config.DiworkEnv;
import com.yonyou.iuap.ucf.multilang.runtime.utils.MlRemoteTool;
import com.yonyou.iuap.yms.id.config.EnableYmsOid;
import com.yonyou.ucf.mdf.sdk.rest.util.PropertyUtil;
import groovy.util.logging.Slf4j;
import org.imeta.spring.support.profile.DomainIsolationPropertyProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableAsync;


/**
 * spring-boot 入口类
 */
@Slf4j
@EnableAsync
@EnableYmsOid
@EnableConfigurationProperties
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, RabbitAutoConfiguration.class})
@ComponentScan(basePackages = {"com.yonyou", "com.yonyoucloud"},
        excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,
                pattern = {"com.yonyoucloud.uretail.*",
                        "com.yonyou.ucf.mdd.core.file.oss.*",
                        "com.yonyou.diwork.multilingual.service.ILanguageService",
                        "com.yonyou.yonbip.iuap.xport.ypd.unified.common.meta.XportPoiSupportApiImpl"})})

@ImportResource({DiworkEnv.DIWORK_CONFIG_XML,"classpath*:/config/applicationContext*.xml","classpath*:/spring-sub/applicationContext*.xml","classpath:applicationContext-imeta.xml", "classpath:applicationContext-common.xml", "classpath:applicationContext-db.xml", "classpath:applicationContext-redis.xml"})
@ServletComponentScan
public class MDFApplication extends SpringBootServletInitializer {
    public static void main(String[] args) throws Exception {
        ApplicationContext app = SpringApplication.run(MDFApplication.class, args);
//        NotifyStub.start();
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        SpringApplicationBuilder applicationBuilder = builder.sources(MDFApplication.class);

        MlRemoteTool.init(PropertyUtil.getProperty("spring.profile"));
        // 缓存通知配置
//        NotifyStub.start();
        return applicationBuilder;
    }

    /**
     * 处理远程参照查询问题,否则会到本地库查询
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public DomainIsolationPropertyProfile domainIsolationPropertyProfile() {
        return new DomainIsolationPropertyProfile();
    }

}